package com.guoyansoft.controller;

import com.guoyansoft.config.PasswordEncryption;
import com.guoyansoft.config.R;
import com.guoyansoft.model.pojo.Address;
import com.guoyansoft.model.pojo.Customer;
import com.guoyansoft.model.pojo.CustomerAndAddress;
import com.guoyansoft.model.pojo.CustomerStar;
import com.guoyansoft.model.service.AddressService;
import com.guoyansoft.model.service.CustomerAndAddressService;
import com.guoyansoft.model.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**@author 常利军
 * 用户业务控制器
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private CustomerAndAddressService customerAndAddressService;
    /**
     * 根据电话号码phone和姓名name进行多条件分页查询
     * @param phone
     * @param name
     * @param curPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryByPage")
    public R queryByPage(String phone,String name,Integer curPage,Integer pageSize){
        Map<String,Object> params=new HashMap<>();
        params.put("phoneKey",phone);
        params.put("nameKey","%"+name+"%");
        params.put("startIndexKey",(curPage-1)*pageSize);
        params.put("pageSizeKey",pageSize);
        R r = customerService.queryByPage(params);
        return r;
    }

    /**
     * 新增
     * @param customer
     * @return
     */
    @RequestMapping("/save")
    public R save(Customer customer,Integer customerStarId,Address address){
        Map<String,Object> map =new HashMap<>();
        map.put("customer",customer);
        map.put("customerStarId",customerStarId);
        map.put("address",address);
        R save = customerService.save(map);
        return save;
    }

    /**
     * 根据id主键删除
     * @param id
     * @return
     */
    @RequestMapping("/drop")
    public R drop(Integer id){
        R drop = customerService.drop(id);
        return drop;
    }

    /**
     * 根据id主键修改
     * @param customer
     * @return
     */
    @RequestMapping("/modify")
    public R modify(Customer customer,Integer customerStarId){
        Map<String,Object> map =new HashMap<>();
        map.put("customer",customer);
        map.put("customerStarId",customerStarId);
        R modify = customerService.modify(map);
        return modify;
    }
    @RequestMapping("/login")
    public R login(String phone,String pass){
        Map<String,String> params=new HashMap<>();
        params.put("phone",phone);
        params.put("pass", PasswordEncryption.getCiphertext(pass));
        R r = customerService.queryByPhone(params);
        return r;
    }
}
